package com.caigou.modules.jingpin.ui

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.openBrowser
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.jingpin.R
import com.caigou.modules.jingpin.adpter.JingpinItemAdapter
import com.caigou.modules.jingpin.bean.JingpinBean
import com.caigou.modules.jingpin.databinding.ActivityJingpinBinding
import com.caigou.modules.jingpin.viewmodel.JingpinViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/03/21
 */
@Route(path = RouterPath.Jingpin.PAGE_JING_PIN)
@AndroidEntryPoint
class JingpinActivity : BaseActivity() {

    private lateinit var binding: ActivityJingpinBinding
    private val jingpinViewModel: JingpinViewModel by viewModels()

    private var page = 1
    private val limit = 10
    private var totalPage = 0

    private val mAdapter: JingpinItemAdapter by lazy {
        JingpinItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityJingpinBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.jingpinLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.jingpinSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                page = 1
                fetchJingpinList()
            }
        }

        mAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                mAdapter.loadMoreModule.isEnableLoadMore = true
                fetchJingpinList()
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }
        binding.jingpinRecyclerView.run {
            layoutManager = LinearLayoutManager(this@JingpinActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@JingpinActivity))
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val url = mAdapter.getItem(position).redirectUrl
            openBrowser(url)
        }

        binding.jingpinTvBack.setOnClickListener {
            finish()
        }

        fetchJingpinList()

    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(jingpinViewModel.jingpinListLiveData, ::handleGetJingpinList)
    }

    private fun handleGetJingpinList(result: Resource<JingpinBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.jingpinSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取精品帖子信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.jingpinSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                totalPage = result.data!!.totalPage

                if (page == 1) {
                    mAdapter.setList(result.data!!.list)
                } else {
                    mAdapter.addData(result.data!!.list)
                }

                if (result.data!!.pageSize < limit) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    mAdapter.loadMoreModule.loadMoreComplete()
                }

                if (page >= totalPage) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    page++
                }
            }
        }
    }

    private fun fetchJingpinList() {
        jingpinViewModel.getJingpinList(page, limit)
    }
}